package com.kaka.bus.engine.user.forget;

import android.content.Intent;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.kaka.bus.AppCenter;
import com.kaka.bus.R;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.engine.other.DrainageActivity;
import com.kaka.bus.engine.user.login.LoginActivity;
import com.kaka.bus.util.U;
import com.kaka.bus.util.logger.Logger;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;

/**
 * Created by zhangqi on 2016/10/17.
 * e-mail : ${email}
 * desc :
 */

public class ForgetActivity extends BaseActivity<ForgetPresenter> implements ForgetContract.View {


    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.edt_forget_phone)
    EditText mEdtForgetPhone;
    @BindView(R.id.tv_forget_send)
    TextView mTvForgetSend;
    @BindView(R.id.edt_forget_yzm)
    EditText mEdtForgetYzm;
    @BindView(R.id.edt_forget_pwd)
    EditText mEdtForgetPwd;
    @BindView(R.id.edt_forget_pwda)
    EditText mEdtForgetPwda;
    @BindView(R.id.btn_forget_next)
    AppCompatButton mBtnForgetNext;

    private String mPhone;

    private String mSmsCode;

    @Override
    protected void initView() {
        super.initView();
        mToolbarName.setText("忘记密码");
        mToolbarBack.setOnClickListener(v -> finish());

//        SpannableString ss = new SpannableString("请输入6位数字或字母及组合作为登录密码");//定义hint的值
//        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(12, true);//设置字体大小 true表示单位是sp
//        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        mEdtForgetPwd.setHint(new SpannedString(ss));
    }

    @Override
    protected void initListener() {
        /**
         * 验证码
         */
       RxView.clicks(mTvForgetSend)
                .throttleFirst(3, TimeUnit.SECONDS)
                .subscribe(v -> {
                    phoneIsExist();
                });
        mBtnForgetNext.setOnClickListener(v -> next());
    }

    private void next() {
        Logger.i("重置密码");
        String phone = getText(mEdtForgetPhone);
        String yzm = getText(mEdtForgetYzm);
        String pwd = getText(mEdtForgetPwd);
        String pwda = getText(mEdtForgetPwda);

        /**
         * 检查手机格式
         */
        if (!U.isMobileExact(phone)) {
            showMessage(R.string.enterOkPhone);
            return;
        }

        if (TextUtils.isEmpty(yzm)) {
            showMessage(R.string.yzmnotnull);
            return;
        }

        if (TextUtils.isEmpty(pwd) || TextUtils.isEmpty(pwda)) {
            showMessage(R.string.enter_again);
            return;
        }

        if (pwd.length() != 6 || pwda.length() != 6) {
            showMessage(R.string.veril);
            return;
        }

        if(!pwd.equals(pwda)){
            showMessage(R.string.two);
            return;
        }


        mPresenter.rePassword(phone, pwd,yzm);
    }


    @Override
    public void forgetSuccess() {
        Logger.i("忘记密码成功,去登录界面");
        Intent intent = new Intent(this,LoginActivity.class);
        startActivity(intent);
        finish();
    }

    public void time() {
        showMessage("成功发送验证码");
        mTvForgetSend.setEnabled(false);
        //倒计时60秒
        //每秒赋值
        //提示错误信息
        addSubscription(U.countdownFlowable(59)            //倒计时60秒
                .subscribe(
                        time -> mTvForgetSend.setText("重新发送" + "(" + time + ")"),    //每秒赋值
                        U::errorUtil,             //提示错误信息
                        () -> {
                            mTvForgetSend.setEnabled(true);
                            mTvForgetSend.setText(getString(R.string.reSendyzm));
                        }));
    }

    @Override
    public void phoneIsExist() {
        mPhone =  mEdtForgetPhone.getText().toString();

        /**
         * 检查手机格式
         */
        if(!U.isMobileExact(mPhone)){
            showMessage(R.string.enterOkPhone);
            return;
        }
        mPresenter.isExist(mPhone);
    }

    @Override
    public void phoneState(HttpWrapper<String> wrapper) {
        if(Constant.SUCCESS_CODE.equals(wrapper.code)){
            showMessage("该手机号未注册!");
        }else if(Constant.ERROR_CODE.equals(wrapper.code)){
            sendyzm();
        }else{
            showMessage(wrapper.info);
        }
    }

    private boolean sendyzm() {
        String phone = mEdtForgetPhone.getText().toString();
        /**
         * 检查手机格式
         */
        if (!U.isMobileExact(phone)) {
            showMessage(R.string.enterOkPhone);
            return false;
        }
        return mPresenter.sendyzm(phone, AppCenter.getTokenKey());
    }

    @Override
    public void sendSuccess(HttpWrapper<String> bean) {
        if (Constant.BQSLOSE_CODE.equals(bean.code)){
            BQSLose();
        }else if(Constant.SUCCESS_CODE.equals(bean.code)){
           time();
        }else{
            yzmFaild(bean);
        }
    }

    @Override
    public void rePasswordSuccess(HttpWrapper<String> bean) {
        if(Constant.SUCCESS_CODE.equals(bean.code)){
            forgetSuccess();
            showMessage(bean.info);
        }else{
            showMessage(bean.info);
        }
    }

    @Override
    public void BQSLose() {
        U.toDrainage(this, DrainageActivity.TYPE_REFUSE,Constant.REFUES);
    }

    @Override
    public void yzmFaild(HttpWrapper<String> bean) {
        showMessage(bean.getInfo());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget;
    }
    @Override
    protected boolean needFullScreen() {
        return true;
    }
    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
